package cn.lzy.taskmanagement.email;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @Author: WeiKaiYe
 * @CreateTime: 2023-10-22  16:57
 */


@Service
public class SendEmailService {
    @Autowired
    private JavaMailSenderImpl mailSender;
    @Value("${spring.mail.username}")
    private String from;

    /**
     * 发送纯文本邮件
     *
     * @param to      收件人地址
     * @param subject 邮件标题
     * @param text    邮件内容
     */

    // 纯文本邮件
    public void sendSimpleEmail(String to, String subject, String text) {
        // 定制纯文本邮件信息SimpleMailMessage
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        try { // 发送邮件
            mailSender.send(message);
            System.out.println("纯文本邮件发送成功!!");
        } catch (MailException e) {
            System.out.println("纯文本邮件发送失败 " + e.getMessage());
            e.printStackTrace();
        }
    }

        //单独图片邮件
        public void sendImageEmail (String to, String subject, String text, String rscId, String rscPath){
            try {
                MimeMessage message = mailSender.createMimeMessage();
                MimeMessageHelper helper = new MimeMessageHelper(message, true);
                helper.setFrom(from);
                helper.setTo(to);
                helper.setSubject(subject);
                helper.setText(text, true);
                // 设置邮件静态资源
                FileSystemResource res = new FileSystemResource(new File(rscPath));
                helper.addInline(rscId, res);
                // 发送邮件
                mailSender.send(message);
                System.out.println("图片邮件发送成功！！");
            } catch (MessagingException e) {
                System.out.println("图片邮件发送失败 " + e.getMessage());
                e.printStackTrace();
            }
        }


        //单独附件邮件
        public void sendAttachFileEmail (String to, String subject, String text, String attachmentPath){
            try {
                // 创建一个带附件的消息对象
                MimeMessage message = mailSender.createMimeMessage();
                MimeMessageHelper helper = new MimeMessageHelper(message, true);

                helper.setFrom(from);
                helper.setTo(to);
                helper.setSubject(subject);
                helper.setText(text);

                // 添加附件
                FileSystemResource attachment = new FileSystemResource(new File(attachmentPath));
                helper.addAttachment(attachment.getFilename(), attachment);

                // 发送邮件
                mailSender.send(message);
                System.out.println("附件邮件发送成功!!");
            } catch (Exception ex) {
                System.out.println("附件邮件发送失败 " + ex.getMessage());
                ex.printStackTrace();
            }
        }


        /**
         * 发送复杂邮件（包括静态资源和附件）发送附件邮件、图片邮件
         *
         * @param to      收件人地址
         * @param subject 邮件标题
         * @param text    邮件内容
         *                发送附件、图片、文本邮件
         */

        // 发送附件、图片、文本邮件  复杂邮件
        public void sendComplexEmail (String to, String subject, String text, String filePath, String rscId, String
        rscPath){
            // 定制复杂邮件信息MimeMessage
            MimeMessage message = mailSender.createMimeMessage();
            try {
                // 使用MimeMessageHelper帮助类，并设置multipart多部件使用为true
                MimeMessageHelper helper = new MimeMessageHelper(message, true);
                helper.setFrom(from);
                helper.setTo(to);
                helper.setSubject(subject);
                helper.setText(text, true);
                // 设置邮件静态资源
                FileSystemResource res = new FileSystemResource(new File(rscPath));
                helper.addInline(rscId, res);
                // 设置邮件附件
                FileSystemResource file = new FileSystemResource(new File(filePath));
                String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
                helper.addAttachment(fileName, file);
                // 发送邮件
                mailSender.send(message);
                System.out.println("复杂邮件发送成功！！");
            } catch (MessagingException e) {
                System.out.println("复杂邮件发送失败 " + e.getMessage());
                e.printStackTrace();
            }
        }


        /**
         * 发送模板邮件
         *
         * @param to      收件人地址
         * @param subject 邮件标题
         * @param content 邮件内容
         */

        public void sendTemplateEmail (String to, String subject, String content){
            MimeMessage message = mailSender.createMimeMessage();
            try {// 使用MimeMessageHelper帮助类，并设置multipart多部件使用为true
                MimeMessageHelper helper = new MimeMessageHelper(message, true);
                helper.setFrom(from);
                helper.setTo(to);
                helper.setSubject(subject);
                helper.setText(content, true);
                // 发送邮件
                mailSender.send(message);
                System.out.println("模板邮件发送成功!!");
            } catch (MessagingException e) {
                System.out.println("模板邮件发送失败 " + e.getMessage());
                e.printStackTrace();
            }
        }

    }